<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //数组去重

        //第一种 用set()方法
        let arr = [10, 20, 20, 40, 40, 50, 80, 80]
        /*         let newVal = new Set(arr)
                let newArr = [...newVal] 
        let newArr = [...new Set(arr)]*/

        //第二种 Array.from()
        /*   let newVal = new Set(arr)
          let newArr = Array.from(newVal, (curval, mapThis) => {
              console.log('当前值', curval)
              console.log('索引', mapThis)
              return curval
          }) */

        /*    let newArr = Array.from(new Set(arr))
           console.log(newArr)
    */

        //第三种 reduce
        /* let newArr = arr.reduce((preVal, curVal) => {
            if (preVal.indexOf(curVal) === -1) preVal.push(curVal)
            return preVal
        }, [])
        console.log(newArr) */

        let newArr = arr.reduce((preVal, curVal) => preVal.indexOf(curVal) === -1 ? preVal.concat([curVal]) : preVal, [])
        console.log(newArr)
    </script>
</body>

</html>